﻿using System;

namespace Toolkit.Web.Infastructure
{
    public abstract class ViewComponentBuilderBase<TViewComponent> where TViewComponent : ViewComponentBase
    {
        protected ViewComponentBuilderBase(TViewComponent component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            Component = component;
        }

        protected TViewComponent Component
        {
            get;
            private set;
        }

        public virtual void Render()
        {
            Component.Render();
        }

        /// <summary>
        /// Automatically call render for convinience and to prevent problems with returning a String vs an IHtmlString.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            Render();
            return null;
        }

        public virtual string ToHtmlString()
        {
            return Component.ToString();
        }
    }
}
